package project;
import java.awt.EventQueue;
import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.Timer;
import java.util.TimerTask;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**对输入框合法时间的判断只做了一部分，识别简单的非法输入。
 * @author Administrator
 *
 */
public class MyTimer extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private Timer timer;
	private MyTask myTask;
	private JLabel label_1;
	private JButton button_4;
	public static JLabel lblNewLabel;
	public static int num=0;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MyTimer frame = new MyTimer();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	/**
	 * Create the frame.
	 */
	public MyTimer() {
		try {
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		} catch (ClassNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (InstantiationException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IllegalAccessException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (UnsupportedLookAndFeelException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		GridBagLayout gbl_contentPane = new GridBagLayout();
		gbl_contentPane.columnWidths = new int[]{0, 0};
		gbl_contentPane.rowHeights = new int[]{0, 0, 0, 0};
		gbl_contentPane.columnWeights = new double[]{1.0, Double.MIN_VALUE};
		gbl_contentPane.rowWeights = new double[]{0.0, 0.0, 0.0, Double.MIN_VALUE};
		contentPane.setLayout(gbl_contentPane);
		
		JPanel panel = new JPanel();
		GridBagConstraints gbc_panel = new GridBagConstraints();
		gbc_panel.insets = new Insets(0, 0, 5, 0);
		gbc_panel.fill = GridBagConstraints.BOTH;
		gbc_panel.gridx = 0;
		gbc_panel.gridy = 0;
		contentPane.add(panel, gbc_panel);
		
		JLabel label = new JLabel("购物狂欢倒计时");
		label.setFont(new Font("楷体",Font.PLAIN,26));
		panel.add(label);
		
		JPanel panel_1 = new JPanel();
		GridBagConstraints gbc_panel_1 = new GridBagConstraints();
		gbc_panel_1.insets = new Insets(0, 0, 5, 0);
		gbc_panel_1.fill = GridBagConstraints.BOTH;
		gbc_panel_1.gridx = 0;
		gbc_panel_1.gridy = 1;
		contentPane.add(panel_1, gbc_panel_1);
		
		label_1 = new JLabel("剩余时间:");
		panel_1.add(label_1);
		
		lblNewLabel = new JLabel("0:0:0:0");
		lblNewLabel.setFont(new Font("微软雅黑",Font.PLAIN,88));
		
		panel_1.add(lblNewLabel);
		
		textField = new JTextField();
		panel_1.add(textField);
		textField.setColumns(10);
		textField.addFocusListener(new FocusListener() {
			
			@Override
			public void focusLost(FocusEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void focusGained(FocusEvent e) {
				// TODO Auto-generated method stub
				label_1.setText("时间格式11:11:11:11");
			}
		});
		
		JButton button_3 = new JButton("确定");
		button_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String time = textField.getText();
				if(stringToNum(time)==-1){
					label_1.setText("时间格式错误");
					return;
				}
				num = stringToNum(time);
				label_1.setText("剩余时间:");
				lblNewLabel.setVisible(true);
				textField.setVisible(false);
				button_3.setVisible(false);
				button_4.setVisible(false);
			}
		});
		panel_1.add(button_3);
		
		button_4 = new JButton("取消");
		button_4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				label_1.setText("剩余时间:");
				lblNewLabel.setVisible(true);
				textField.setVisible(false);
				button_3.setVisible(false);
				button_4.setVisible(false);
			}
		});
		panel_1.add(button_4);
		button_3.setVisible(false);
		button_4.setVisible(false);
		textField.setVisible(false);
		
		JPanel panel_2 = new JPanel();
		GridBagConstraints gbc_panel_2 = new GridBagConstraints();
		gbc_panel_2.fill = GridBagConstraints.BOTH;
		gbc_panel_2.gridx = 0;
		gbc_panel_2.gridy = 2;
		contentPane.add(panel_2, gbc_panel_2);
		
		JButton button = new JButton("设置时间");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				label_1.setText("时间格式11:11:11:11");
				lblNewLabel.setVisible(false);
				textField.setVisible(true);
				button_3.setVisible(true);
				button_4.setVisible(true);
				
			}
		});
		panel_2.add(button);
		
		JButton button_1 = new JButton("暂停");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				timer.cancel();
				timer=null;
			}
		});
		panel_2.add(button_1);
		
		JButton button_2 = new JButton("开始");
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(timer==null){
					timer = new Timer();
					myTask = new MyTask();
					timer.schedule(myTask,0,1000);
				}
			}
		});
		panel_2.add(button_2);
		num = 86399;
		myTask = new MyTask();
		timer = new Timer(); 
	    timer.schedule(myTask,0,1000);
	}
	
	public int stringToNum(String str){
		int num = 0;
		int index[] = {86400,3600,60,1};
		String s[] = str.split(":");
		for(int i=0;i<s.length;i++){
			try{
				if(Integer.parseInt(s[i])>59){
					return -1;
				}
				num +=Integer.parseInt(s[i])*index[i];
			}catch(Exception e){
				return -1;
			}
		}
		return num;
	}

}

class MyTask extends TimerTask{
	public void run() {
		MyTimer.lblNewLabel.setText(numToTime(MyTimer.num));
		MyTimer.num--;
	}
	public String numToTime(int num){
		String result="";
		int d=0,h=0,m=0,s=0;
		s=num%60;
		m=num/60%60;
		h=num/3600%24;
		d=num/86400;
		result = ""+d+":"+h+":"+m+":"+s;
		if(num>8640000)
			result="超过100天";
		return result;
	}
}